package org.duang.tags;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.duang.config.Config;
import org.duang.kit.ToolsKit;

import com.aliyun4j.utils.Tools;



@SuppressWarnings("serial") 
public class WebUploader extends BodyTagSupport {	
	 
	private String product;
	private String flag;
	private String iszip;
	private List<String> pics;
	private String path;
	private String createuserid;

	/**
	 * 	是否执行自定义标签的标签体,是否创建一个缓冲区对象捕获标签体的执行结果,从而让标签开发者在标签处理种对标签体的执行结果进行修改,其返回值可以是一下三个:
		EVAL_BODY_INCLUDE(执行标签体),
		SKIP_BODY(跳过标签体),
		EVAL_BODY_BUFFERED(把标签体放到缓冲区中,以便进一步处理)
	 */
	@Override
	public int doStartTag() throws JspException { 
		try {
			product = (ToolsKit.isEmpty(product)) ? Config.getProductCode() : product;
			flag = (ToolsKit.isEmpty(flag)) ? "upload" : flag;
			iszip = (ToolsKit.isEmpty(iszip)) ? "1" : iszip;
			pics = (ToolsKit.isEmpty(pics)) ? null : pics;
			path = (ToolsKit.isEmpty(path)) ? null : path;
			createuserid = (ToolsKit.isEmpty(createuserid)) ? null : createuserid; 
			pageContext.getOut().write(buildUploadHtml());
//			pageContext.getOut().write(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//return EVAL_BODY_INCLUDE; 	//执行doAfterBody()方法
//		return EVAL_BODY_BUFFERED;
		return SKIP_BODY;
	}
	
	/**
	 *  EVAL_BODY_AGAIN(表示再执行标签体一次),
		SKIP_BODY(表示结束标签体执行继续执行doEndTag)
	 */
	@Override  
     public int doAfterBody() throws JspException {		 
		return SKIP_BODY;  		//表示结束标签体执行继续执行doEndTag
     }  
	

    /**
     * 控制是否执行结束标签后面的内容, 其返回值可以为:
		EVAL_PAGE(表示继续执行结束标签后面的jsp代码);
		SKIP_PAGE(表示忽略结束标签后面的jsp代码).
     */
     @SuppressWarnings("static-access")
	@Override  
     public int doEndTag() throws JspException {
         return this.EVAL_PAGE;  
     }

     //构建HTML
     public String buildUploadHtml(){ 
    	 StringBuilder uploadStr = new StringBuilder();
    	 uploadStr.append("<fieldset class='blue pad-10 fieldset'>")
    	 		  .append("<legend>图片列表</legend>")
    	 		  .append("<div class='picList' id='").append(product).append(flag).append("_img_div'></div>")
    	 		  .append("</fieldset>")
    	 		  .append("<div class='bk10'></div>")
    	 		  .append("<a class='btn btn-default' onclick=\"javascript:openWebUploaderWin('").append(product).append("', '").append(flag).append("', '").append(iszip).append("')\" herf='javascript:void(0);'><span class='add'></span>选择图片 </a>");
    	 StringBuilder sb = new StringBuilder();
    	 if(Tools.isNotEmpty(pics) && Tools.isEmpty(path)){
    		 for (String path : pics){
				sb.append(getFileName(path)).append(",");
			}
    		 if(sb.length()>1) sb.deleteCharAt(sb.length()-1);
    	 }
    	 if(Tools.isNotEmpty(path) && Tools.isEmpty(pics)){
    		 sb.append(getFileName(path));
    	 }
    	 if(sb.length()>1){
    		 uploadStr.append("<div style='display:none;' id='webuploader_hiddens_").append(product).append(flag).append("'>");
    		 uploadStr.append("<input type='hidden' id='webuploader_pics_").append(product).append(flag).append("' value='").append(sb).append("'/>");
    		 uploadStr.append("<input type='hidden' id='webuploader_createuserid_").append(product).append(flag).append("' value='").append(createuserid).append("'/>");
    		 uploadStr.append("<script>init_webuploader_li('"+product+"', '"+flag+"')</script>");
    		 uploadStr.append("</div>");
    	 }
    	 return uploadStr.toString();
     }
     
     
    private String getFileName(String path){
    	if(path.indexOf("\\") > -1){
    		path = path.replace("\\", "/");
    	}
    	return path;
//    	return path.substring(path.lastIndexOf(Const.getSeparator())+1, path.length());
    }

	public String getProduct() {
		return product;
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getIszip() {
		return iszip;
	}

	public void setIszip(String iszip) {
		this.iszip = iszip;
	}

	public List<String> getPics() {
		return pics;
	}

	public void setPics(List<String> pics) {
		this.pics = pics;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
	public String getCreateuserid() {
		return createuserid;
	}

	public void setCreateuserid(String createuserid) {
		this.createuserid = createuserid;
	}
	
}
